import {Component, Input, OnInit} from '@angular/core';
import {CommonPage} from '../../common-page';
import {SelectType} from '../../../../shared/components/dhlcomponent/enums';
import {TreeUtil} from '../../../../shared/utils/tree-util';
import {ObjectUtil} from '../../../../shared/utils/object-util';

@Component({
  templateUrl: './update.user.form.component.html'
})
export class UpdateUserFormComponent extends CommonPage implements OnInit {


  expandKeys = ['ALL', 'DZO', 'JSO', 'MZO', 'NLO', 'PTO'];
  datas = [
    {
      title: '区域',
      key: 'ALL',
      children: [
        {
          title: 'CAO',
          key: 'CAO',
          isLeaf: true
        },
        {
          title: 'DZO',
          key: 'DZO',
          children: [
            {title: 'DGO', key: 'DGO', isLeaf: true},
            {title: 'ZSO', key: 'ZSO', isLeaf: true}
          ]
        },
        {
          title: 'JSO',
          key: 'JSO',
          children: [
            {title: 'NKO', key: 'NKO', isLeaf: true},
            {title: 'SUO', key: 'SUO', isLeaf: true}
          ]
        },
        {
          title: 'MZO',
          key: 'MZO',
          children: [
            {title: 'HGO', key: 'HGO', isLeaf: true},
            {title: 'NGO', key: 'NGO', isLeaf: true},
            {title: 'FJO', key: 'FJO', isLeaf: true}
          ]
        },
        {
          title: 'NLO',
          key: 'NLO',
          children: [
            {title: 'SDO', key: 'SDO', isLeaf: true},
            {title: 'NEO', key: 'NEO', isLeaf: true},
            {title: 'NWO', key: 'NWO', isLeaf: true},
            {title: 'NRO', key: 'NRO', isLeaf: true},
            {title: 'SWO', key: 'SWO', isLeaf: true}
          ]
        },
        {
          title: 'PTO',
          key: 'PTO',
          children: [
            {title: 'PEO', key: 'PEO', isLeaf: true},
            {title: 'TSO', key: 'TSO', isLeaf: true}
          ]
        },
        {
          title: 'SHO',
          key: 'SHO',
          isLeaf: true
        },
        {
          title: 'SZO',
          key: 'SZO',
          isLeaf: true
        }
      ]
    }
  ];
  @Input() id;
  @Input() name;
  @Input() uuid;
  @Input() projectId;
  @Input() projectCode;
  @Input() userIds;
  @Input() roleOptions;
  @Input() roleId;
  @Input() areaCodes;
  @Input() areaOptions;
  @Input() selectType = SelectType.DEFAULT;
  @Input() showDataPer = false;
  @Input() email;
  @Input() title;

  @Input() set areaUsers(data) {
    for (const key of Object.keys(data)) {
      const value = data[key];
      const node = TreeUtil.findNodeByKey(this.datas, key);
      if (node) {
        // @ts-ignore
        const users = value.map(p => ({key: key + '-' + p.split('-')[0], title: p, isLeaf: true}));
        node.children = users;
        node.isLeaf = false;
      }
    }
  };

  selectChange(key) {
    this.showDataPer = false;
    const options = this.roleOptions;
    if (this.projectCode && this.projectCode == 'rpmp') {

      if (this.roleOptions) {
        options.forEach((row) => {
          if (ObjectUtil.isArray(key)) {
            key.forEach((k) => {
              if (row.value == k && (row.name == '区域用户-查询' || row.name == '区域管理员')) {
                this.showDataPer = true;
              }
            });
          } else if (row.value == key && (row.name == '区域用户-查询' || row.name == '区域管理员')) {
            this.showDataPer = true;
          }
        });
      }
    }
  }


  ngOnInit(): void {

    this.filter(this.datas[0]);

    this.selectChange(this.roleId);
  }

  filter(option) {
    if (this.areaOptions) {
      for (const value of this.areaOptions) {
        if (option.key == value) {
          return true;
        }
      }
    }

    if (option.children) {
      const newChildren = [];
      for (const child of option.children) {
        const flag = this.filter(child);
        if (flag) {
          newChildren.push(child);
        }
      }
      option.children = newChildren;
      if (newChildren.length > 0) {
        return true;
      } else {
        return false;
      }
    } else {
      return false;
    }
  }

  onExpand({key}) {
    this.requestService.get(`projectuser/users/${key}`).subscribe(data => {
      if (data) {
        const node = TreeUtil.findNodeByKey(this['nodes'], key);
        const users = data.map(p => ({key: key + '-' + p.uuid, title: p.uuid, isLeaf: true}));
        node.children = users;
        this['nodes'] = [...this['nodes']];
      }
    });
  }


}




